﻿/**************************************************************
 *
 * 唯一标识：47d4ea4f-9257-467f-913a-1db9f859f85f
 * 命名空间：Sgr.UPMS.Application.Commands.Duties
 * 创建时间：2023/8/27 19:24:00
 * 机器名称：DESKTOP-S0D075D
 * 创建者：antho
 * 电子邮箱：fengqinhua2016@163.com
 * 描述：删除职务命令处理器
 *
 **************************************************************/

using MediatR;
using Sgr.Application.Commands;
using Sgr.UPMS.Domain.Duties;
using System.Threading;
using System.Threading.Tasks;

namespace Sgr.UPMS.Application.Commands.Duties
{
    /// <summary>
    /// 删除职务命令处理器
    /// </summary>
    public class DeleteDutyCommandHandle : IRequestHandler<DeleteDutyCommand, CommandResult<bool>>
    {
        private readonly IDutyRepository _dutyRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dutyRepository">职务仓储</param>
        public DeleteDutyCommandHandle(IDutyRepository dutyRepository)
        {
            _dutyRepository = dutyRepository;
        }

        /// <summary>
        /// 处理删除职务命令
        /// </summary>
        /// <param name="request">命令请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>处理结果</returns>
        public async Task<CommandResult<bool>> Handle(DeleteDutyCommand request, CancellationToken cancellationToken)
        {
            // 查找职务实体
            var entity = await _dutyRepository.GetAsync(request.DutyId, cancellationToken);

            // 如果找不到职务，返回NotFound结果
            if (entity == null)
                return CommandResult.NotFound<bool>("未找到指定的职务");

            // 标记职务为删除状态
            entity.MarkForDeletion();

            // 从仓储中删除职务
            await _dutyRepository.DeleteAsync(entity, cancellationToken);

            // 返回成功结果
            return CommandResult.Success(true, "职务删除成功");
        }
    }
}